#!/usr/bin/env bash
set -x
IMAGE_DUMP_DIR=/home/dig/graph_data/image_data
REMOTE_ADDR=172.18.180.228

version=`date +%Y%m%d -d "1 day ago"`

TAR_COLLECTIONS=" tradable_share Court_announcement_doc Bid_detail Person Judgement_wenshu used_name Company Court_bulletin_doc sue Judge_process  belong  agent_bid invest win_bid officer publish_bid"

cd ${IMAGE_DUMP_DIR}
dir="dir"
echo ${TAR_COLLECTIONS}|tr " " " $dir/"|cat
dirs=`ls -a |grep '^data_parsed_20'|grep -v tar|awk '{print $NF}'`

# arangodb
for dir in ${dirs}
do
    if [ -d "${dir}" ];then
        if [ -f ${dir}/lz ]; then
            continue
        fi
        mkdir -p ${dir}_lz/image
        echo "poc_data_lanzhou" > ${dir}_lz/db_name.txt
        cp ${dir}/source_version ${dir}_lz/
        for col in ${TAR_COLLECTIONS}
        do 
            cp -rf ${dir}/image/${col}_txt ${dir}_lz/image
        done
        rm ${dir}_lz/image/HBASE_IMPORTED
        tar -zcf ${dir}_lz.tar.gz ${dir}_lz
        remote_dir=/home/nfs/server-download-dir/arangodb-file/${version}
        #ssh -p 33982 work@120.76.198.171 "mkdir "${remote_dir}
        ssh work@${REMOTE_ADDR} "mkdir -p "${remote_dir}

        local_md5=`cat ${dir}_lz.tar.gz | md5sum | awk '{print $1}'`
        try_cnt=0
        while [ ${try_cnt} -lt 4 ];
        do
            #rsync --append --partial --progress -e "ssh -p33982" ${dir}_lz.tar.gz work@120.76.198.171:${remote_dir}/
            rsync --append --partial --progress -e "ssh -p22" ${dir}_lz.tar.gz work@${REMOTE_ADDR}:${remote_dir}/
            remote_md5=`ssh work@${REMOTE_ADDR} cat ${remote_dir}/${dir}_lz.tar.gz | md5sum | awk '{print $1}'`
            if [ "$remote_md5" != "$local_md5" ];then
                echo "传送${dir}_lz.tar.gz ${version} 失败, MD5不一致: remote_md5 = ${remote_md5} local_md5 = ${local_md5}"
            else
                break
            fi
            try_cnt=`expr ${try_cnt} + 1`
        done

        if [ ${try_cnt} -lt 4 ]; then
            rm -rf ${dir}_lz
            rm ${dir}_lz.tar.gz
            echo "${version}/${dir}_lz.tar.gz 数据传输成功，删除数据"
        else
            echo "${version}/${dir}_lz.tar.gz 数据传输失败"
        fi
        tar -zcf ${dir}.tar.gz ${dir} 
        touch ${dir}/lz
    fi
done

#tag
#ssh -p 33982 work@120.76.198.171 "mv /home/nfs/lanzhou-tmp/tag-tmp/* /home/nfs/server-download-dir/label-data-new"
ssh work@${REMOTE_ADDR} "mv /home/nfs/lanzhou-tmp/tag-tmp/* /home/nfs/server-download-dir/label-data-new"

# correct data
cd ${IMAGE_DUMP_DIR}/..
correct_remote_dir=/home/nfs/server-download-dir/arangodb-correct/${version}
tar -zcf data_correct.tar.gz correct_data
ssh work@${REMOTE_ADDR} "mkdir -p "${correct_remote_dir}

# 计算MD5
local_md5=`cat ${IMAGE_DUMP_DIR}/../data_correct.tar.gz | md5sum | awk '{print $1}'`

try_cnt=0
while [ ${try_cnt} -lt 6 ];
do
    # 这里需要加一个重试校验机制
    #先删除源文件
    ssh work@${REMOTE_ADDR} rm -f ${correct_remote_dir}/data_correct.tar.gz
    rsync --append --partial --progress -e "ssh -p22" ${IMAGE_DUMP_DIR}/../data_correct.tar.gz work@${REMOTE_ADDR}:${correct_remote_dir}/
    remote_md5=`ssh work@${REMOTE_ADDR} cat ${correct_remote_dir}/data_correct.tar.gz | md5sum | awk '{print $1}'`
    if [ "$remote_md5" != "$local_md5" ];then
        echo "传送data_correct ${version} 失败, MD5不一致: remote_md5 = ${remote_md5} local_md5 = ${local_md5}"
    else
        break
    fi
    try_cnt=`expr ${try_cnt} + 1`
done

if [ ${try_cnt} -lt 6 ]; then
    echo "data_correct.tar.gz ${version} 传送成功"
else
    echo "data_correct.tar.gz ${version} 传送失败"
fi

mkdir -p correct_data_tar_bak
cp data_correct.tar.gz correct_data_tar_bak/data_correct.tar.gz_${version}
rm data_correct.tar.gz




